package com.xm.serviceimpl;

import com.xm.common.utils.idworker.Sid;
import com.xm.mapper.*;
import com.xm.pojo.Users;
import com.xm.pojo.UsersFans;
import com.xm.pojo.UsersLikeVideos;
import com.xm.pojo.UsersReport;
import com.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper userMapper;

    @Autowired
    private UsersLikeVideosMapper usersLikeVideosMapper;

    @Autowired
    private UsersCustomMapper usersCustomMapper;

    @Autowired
    private UsersFansMapper usersFansMapper;

    @Autowired
    private UsersReportMapper usersReportMapper;

    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean userIsExists(String username) {
        Users user = new Users();
        user.setUsername(username);

        Users result = userMapper.selectOne(user);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void reportUser(UsersReport usersReport) {
        String urId = sid.nextShort();
        usersReport.setId(urId);
        usersReport.setCreateDate(new Date());
        usersReportMapper.insert(usersReport);

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isFollow(String loginUserId, String beFocusUserId) {
        Example example = new Example(UsersFans.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",beFocusUserId);
        criteria.andEqualTo("fanId",loginUserId);
        List<UsersFans> list = usersFansMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUser(Users user) {
        String id = sid.nextShort();
        user.setId(id);
        user.setAdmin(0);
        userMapper.insert(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users userLogin(Users user) {
        Users resUser = userMapper.selectOne(user);
        return resUser == null ? null : resUser;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void FocusOnUser(String loginUserId, String beFocusUserId) {
        //1、累加登录用户的关注数
        usersCustomMapper.addFollowCounts(loginUserId);
        //2、累加被关注粉丝数
        usersCustomMapper.addUserFans(beFocusUserId);
        //3、增加被关注者-粉丝关系
        UsersFans usersFans = new UsersFans();
        usersFans.setId(sid.nextShort());
        usersFans.setUserId(beFocusUserId);
        usersFans.setFanId(loginUserId);
        usersFansMapper.insert(usersFans);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void UnFocusOnUser(String loginUserId, String beFocusUserId) {
        //1、累减登录用户的关注数
        usersCustomMapper.reduceFollowCounts(loginUserId);
        //2、累减被关注粉丝数
        usersCustomMapper.reduceUserFans(beFocusUserId);
        //3、删除被关注者-粉丝关系
        Example example = new Example(UsersFans.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",beFocusUserId);
        criteria.andEqualTo("fanId",loginUserId);
        usersFansMapper.deleteByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isUserLikeVideos(String userId, String videoId) {
        if(StringUtils.isEmpty(userId)){
            return false;
        }
        Example example = new Example(UsersLikeVideos.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("videoId",videoId);
        List<UsersLikeVideos> list = usersLikeVideosMapper.selectByExample(example);
        if(list.size() > 0)return true;
        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updataUserInfo(Users user) {
        Example userexample = new Example(Users.class);
        Example.Criteria criteria = userexample.createCriteria();
        criteria.andEqualTo("id",user.getId());
        userMapper.updateByExampleSelective(user,userexample);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users query(String userId) {
        Example userexample = new Example(Users.class);
        Example.Criteria criteria = userexample.createCriteria();
        criteria.andEqualTo("id",userId);
        Users user = userMapper.selectOneByExample(userexample);
        return user;
    }

}
